import Vue from 'vue';

const defaultPartData = {
  // 部分的基础信息
  form: {
    title: '',
    description: ''
  },
  // 部分的分数配置
  scores: {
    question_type_1: 1,
    question_type_2: 1,
    question_type_3: 1,
    question_type_3_miss: 0,
    question_type_4: 1,
    question_type_4_miss: 0,
    question_type_5: 1,
    question_type_8: 1
  },
  topics: []
}

const state = {
  parts: []
}

const mutations = {
  CREATE_PART: (state, data) => {
    state.parts.push(data);
  },
  REMOVE_PART: (state, index) => {
    state.parts.splice(index, 1);
  },
  INSERT_PART: (state, {
    index,
    data
  }) => {
    state.parts.splice(index, 0, data);
  },
  SET_PART_DATA: (state, {
    partIndex,
    data
  }) => {
    Vue.set(state.parts, partIndex, data);
  },
  CLEAR_PARTS: (state) => {
    state.parts = [];
    // const item = JSON.parse(JSON.stringify(defaultPartData))
    // Vue.set(state.parts, 0, item);
  },
  REMOVE_TOPIC: (state, {
    partIndex,
    topicId
  }) => {
    const data = state.parts[partIndex].topics;
    const index = data.findIndex(item => item.topic.id == topicId)
    data.splice(index, 1);
    Vue.set(state.parts[partIndex], 'topics', data);
  },
  REMOVE_TOPIC_ALL: (state, {
    partIndex,
    topic
  }) => {
    const data = state.parts[partIndex].topics; // 原数据
    // 如果选中数据跟原数据某一条标识相等，删除对应的原数据
    topic.forEach((item) => {
      data.forEach((itemI, indexI) => {
        if (item.topic.id === itemI.topic.id) {
          data.splice(indexI, 1);
        }
      });
    });
  },
  SETTING_TOPIC_SCORES: (state, {
    partIndex,
    updateAll = true,
    topicIds = []
  }) => {
    const data = state.parts[partIndex].topics;
    const score = Object.keys(state.parts[partIndex].scores).length > 0 ? state.parts[partIndex].scores : defaultPartData.scores;
    data.forEach((item) => {
      if (topicIds.length > 0 && !topicIds.includes(item.topic.id)) {
         return item;
      }
      const queType = {
        1: score.question_type_1,
        2: score.question_type_2,
        3: score.question_type_3,
        4: score.question_type_4,
        5: score.question_type_5,
        6: score.question_type_6,
        7: score.question_type_7,
        8: score.question_type_8
      }
      // 填空题
      if (item.topic.question_type == 5) {
        item.score = queType[item.topic.question_type] * item.topic.options.length
      } else if (item.topic.question_type == 6 || item.topic.question_type == 7) {
        // 小题
        item.topic.options.forEach(res => {
          res.score = updateAll ? queType[res.question_type] : res.score;
        });
        item.score = sum(item.topic.options.map(res => res.score));
      } else {
        item.score = queType[item.topic.question_type]
      }
    });
  },
  INIT_PARTS: (state, data) => {
    state.parts = data;
  }
}

const actions = {
  // 添加部分
  addPart({
    commit
  }, data) {
    commit('CREATE_PART', data)
  },
  // 移除部分
  removePart({
    commit
  }, index) {
    commit('REMOVE_PART', index)
  },
  // 移动部分
  movePart({
    state,
    commit
  }, {
    oldIndex,
    newIndex
  }) {
    const data = state.parts[oldIndex];
    // 移除
    commit('REMOVE_PART', oldIndex)
    // 增加
    commit('INSERT_PART', {
      index: newIndex,
      data
    });
  },
  // 设置部分内容
  setPart({
    commit
  }, data) {
    commit('SET_PART_DATA', data)
  },
  // 清空部分
  clearParts({
    commit
  }) {
    commit('CLEAR_PARTS');
  },
  // 移除试题
  removeTopic({
    commit
  }, data) {
    commit('REMOVE_TOPIC', data)
  },
  // 批量移除试题
  removeTopicAll({
    commit
  }, data) {
    commit('REMOVE_TOPIC_ALL', data)
  },
  // 批量设置部分的试题分数
  settingTopicScores({
    commit
  }, data) {
    commit('SETTING_TOPIC_SCORES', data)
  },
  // 初始化数据
  initParts({
    commit
  }, data) {
    const parts = [];
    // 组装数据
    data.forEach(part => {
      const topics = [];
      const score = Object.keys(part.scores).length > 0 ? part.scores : defaultPartData.scores;
      const queType = {
        1: score.question_type_1,
        2: score.question_type_2,
        3: score.question_type_3,
        4: score.question_type_4,
        5: score.question_type_5,
        6: score.question_type_6,
        7: score.question_type_7,
        8: score.question_type_8
      }
      if (part.value) {
        part.value.forEach(item => {
          const topic = item.topic;
          // 填空题
          if (topic.question_type == 5) {
            item.score = queType[topic.question_type] * topic.options.length
          } else if (topic.question_type == 6 || topic.question_type == 7) {
            topic.options.forEach(i => {
              const sub = item.sub_topics.find(sub => sub.topic_id == i.id);
              if (sub) {
                i.score = sub.score;
              } else {
                i.score = defaultPartData.scores['question_type_' + i.question_type]
              }
            })
          }
          topics.push({
            score: item.score,
            topic: topic
          })
        })
      }
      parts.push({
        // 部分的基础信息
        form: {
          title: part.title,
          description: part.description
        },
        // 部分的分数配置
        scores: part.scores == null ? defaultPartData.scores : part.scores,
        topics: topics
      })
    })
    // 更新数据
    commit('INIT_PARTS', parts);
  }
}

/** 累加器 **/
const sum = (...arr) => [].concat(...arr).reduce((acc, val) => acc + val, 0);
const getters = {
  // 部分数据
  parts: state => state.parts,
  // 试卷总题数
  topicsTotalCount: state => {
    const parts = state.parts;
    const arr = parts.map(part => part.topics.length);
    return sum(arr);
  },
  // 试卷总分数
  topicsTotalScore: state => {
    const parts = state.parts;
    const arr = parts.map(part => {
      return sum(part.topics.map(topic => {
        return topic.score
      }));
    });
    return sum(arr);
  },
  // 试题部分的提交数据
  partsSubmitData: state => {
    const parts = state.parts;
    const data = [];
    parts.forEach((part, index) => {
      const topics = part.topics.map(item => {
        const res = {
          score: item.score,
          topic_id: item.topic.id
        }
        if (item.topic.question_type == 6 || item.topic.question_type == 7) {
          res.sub_topics = item.topic.options.map(subTopic => {
            return {
              score: subTopic.score,
              topic_id: subTopic.id
            }
          })
        }
        return res;
      });
      data.push({
        number: index + 1,
        title: part.form.title,
        description: part.form.description,
        scores: part.scores,
        topics: topics
      })
    })

    return data;
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
